package com.dreamlin.basepro.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat;
import androidx.transition.TransitionManager;

import com.dreamlin.basepro.R;
import com.dreamlin.basepro.common.DensityUtils;

public class QuickNavigationBar extends ViewGroup implements View.OnClickListener {

    private int itemWidth = DensityUtils.dp2px(15);
    private int itemHeight = DensityUtils.dp2px(15);
    private int itemMarginTop = 0;
    private int itemPaddingLeft = 0;
    private int itemPaddingTop = 0;
    private int itemPaddingRight = 0;
    private int itemPaddingBottom = 0;
    private int itemTextSize = 10;
    private int itemTextColor = 0xFF6E8CA0;
    private int itemSelectedBgColor = 0x00EE00;
    private Drawable selectedDrawable;

    private int lastSelectIndex = 0;
    private static String[] characters = new String[]{
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"
    };
    private ItemSelectListener listener;

    public QuickNavigationBar(Context context) {
        this(context, null);
    }

    public QuickNavigationBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QuickNavigationBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        selectedDrawable = ContextCompat.getDrawable(getContext(), R.drawable.shape_select_green);
        for (int i = 0; i < characters.length; i++) {
            TextView textView = new TextView(getContext());
            LayoutParams layoutParams = new LayoutParams(itemWidth, itemHeight);
            textView.setGravity(Gravity.CENTER);
            textView.setTextSize(itemTextSize);
            textView.setTextColor(itemTextColor);
            textView.setPadding(itemPaddingLeft, itemPaddingTop, itemPaddingRight, itemPaddingBottom);
            textView.setLayoutParams(layoutParams);
            textView.setText(characters[i]);
            textView.setTag(i);
            textView.setOnClickListener(this);
            addView(textView);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        int height = characters.length * itemHeight;
        height = resolveSize(height, heightMeasureSpec);
        int width = itemWidth + getPaddingLeft() + getPaddingRight();
        width = resolveSize(width, widthMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int top = itemMarginTop;
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            childAt.layout(0, top, itemWidth, top + itemHeight);
            top += itemHeight + itemMarginTop;
        }
    }

    @Override
    public void onClick(View v) {
        int selectIndex = (int) v.getTag();
        if (selectIndex >= 0 && selectIndex < characters.length) {
            selectItem(selectIndex, false);
            if (listener != null) {
                listener.onItemClicked(characters[selectIndex]);
            }
        }
    }

    private void selectItem(int selectIndex, boolean smoothScroll) {
        if (lastSelectIndex == selectIndex) return;
        if (smoothScroll) {
            TransitionManager.beginDelayedTransition(this);
        }
        if (lastSelectIndex >= 0 && lastSelectIndex < characters.length) {
            getChildAt(lastSelectIndex).setBackgroundColor(Color.TRANSPARENT);
        }
        ViewCompat.setBackground(getChildAt(selectIndex), selectedDrawable);
        lastSelectIndex = selectIndex;
        if (listener != null) {
            listener.onItemSelected(characters[selectIndex]);
        }
    }

    /**
     * 滚动到
     *
     * @param character
     */
    public void scrollTo(String character) {
        scrollTo(character, false);
    }

    public void scrollTo(String character, boolean smoothScroll) {
        int index = findIndex(character);
        if (index >= 0 && index < characters.length) {
            selectItem(index, smoothScroll);
        }
    }

    private static int findIndex(String character) {
        if (character.equals("#"))
            return characters.length - 1;
        int compareTo = character.compareTo(characters[0]);
        return compareTo >= 0 && compareTo < characters.length - 1 ? compareTo : -1;
    }

    public void setListener(ItemSelectListener listener) {
        this.listener = listener;
    }

    public interface ItemSelectListener {
        void onItemSelected(String character);

        void onItemClicked(String character);
    }
}
